package nodes.binary;

import java.util.Queue;
import model.Model;
import nodes.AbstractNode;


/**
 * Represents a node that takes in two arguments (such as multiply or divide)
 * 
 * @author Andrew Patterson
 * @author Roman Zhang
 */
public abstract class AbstractBinaryNode extends AbstractNode
{
    protected AbstractNode myLeft; // Argument 1
    protected AbstractNode myRight; // Argument 2
    protected boolean operandSet; // Has this node been completely created yet?


    public AbstractBinaryNode (Model model)
    {
        super(model);
    }


    /**
     * Uses the parser stack to set the two arguments
     */
    @Override
    public AbstractNode buildTree (Queue<AbstractNode> stack)
    {
        if (!operandSet)
        {
            myRight = stack.poll().buildTree(stack);
            myLeft = stack.poll().buildTree(stack);
            operandSet = true;
        }
        return this;
    }


    public AbstractNode getLeft ()
    {
        return myLeft;
    }


    public AbstractNode getRight ()
    {
        return myRight;
    }


    @Override
    public String toString ()
    {
        return myLeft.toString() + " " + myName + " " + myRight.toString();
    }
}
